/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogAlterarSenha.java
 *
 * Created on Dec 22, 2010, 8:17:43 PM
 */
package br.com.tbp.gui;

import br.com.tbp.controle.ControleUsuario;
import br.com.tbp.exception.PersistenciaException;
import br.com.tbp.model.domain.Usuario;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

/**
 *
 * @author Diego Freire
 */
public class TrocarSenhaDialog extends javax.swing.JDialog {

    public TrocarSenhaDialog(java.awt.Frame parent, boolean modal, long usuarioId) {
        super(parent, "TBP - Trocar Senha", modal);
        initComponents();
        setLocationRelativeTo(null);
        montarUsuario(usuarioId);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextFieldUsuarioId = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldNome = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldLogin = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jPasswordFieldSenhaAtual = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jPasswordFieldNovaSenha = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        jPasswordFieldRepitaNovaSenha = new javax.swing.JPasswordField();
        fecharButton = new javax.swing.JButton();
        confirmarButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Usuário Id:");

        jTextFieldUsuarioId.setEditable(false);
        jTextFieldUsuarioId.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTextFieldUsuarioId.setFocusable(false);

        jLabel2.setText("Nome:");

        jTextFieldNome.setEditable(false);
        jTextFieldNome.setFocusable(false);

        jLabel7.setText("Login:");

        jTextFieldLogin.setEditable(false);
        jTextFieldLogin.setFocusable(false);

        jLabel8.setText("Senha Atual:");

        jLabel3.setText("Nova Senha:");

        jLabel4.setText("Repita Nova Senha:");

        fecharButton.setText("Fechar (ESC)");
        fecharButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecharButtonActionPerformed(evt);
            }
        });

        confirmarButton.setText("Confirmar");
        confirmarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel8)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldUsuarioId, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldNome, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                            .addComponent(jTextFieldLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jPasswordFieldRepitaNovaSenha, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPasswordFieldNovaSenha, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPasswordFieldSenhaAtual, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(confirmarButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fecharButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldUsuarioId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jPasswordFieldSenhaAtual, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jPasswordFieldNovaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jPasswordFieldRepitaNovaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fecharButton)
                    .addComponent(confirmarButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void fecharButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharButtonActionPerformed
        dispose();
    }//GEN-LAST:event_fecharButtonActionPerformed

    private void confirmarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmarButtonActionPerformed
        if (jPasswordFieldSenhaAtual.getPassword().length < 1) {
            JOptionPane.showMessageDialog(null, "A senha atual deve ser informada");
            jPasswordFieldSenhaAtual.requestFocus();
            return;
        } else if (jPasswordFieldNovaSenha.getPassword().length < 1) {
            JOptionPane.showMessageDialog(null, "A nova senha deve ser informada");
            jPasswordFieldNovaSenha.requestFocus();
            return;
        } else if (jPasswordFieldRepitaNovaSenha.getPassword().length < 1) {
            JOptionPane.showMessageDialog(null, "Repita nova senha deve ser informada");
            jPasswordFieldRepitaNovaSenha.requestFocus();
            return;
        } else if (!new String(jPasswordFieldNovaSenha.getPassword()).equals(
                new String(jPasswordFieldRepitaNovaSenha.getPassword()))) {
            JOptionPane.showMessageDialog(null, "Nova senha e repita nova senha não conferem");
            jPasswordFieldRepitaNovaSenha.requestFocus();
            return;
        } else {
            try {
                ControleUsuario controle = new ControleUsuario();
                long alterou = controle.modificarSenha(Long.parseLong(jTextFieldUsuarioId.getText()),
                        new String(jPasswordFieldSenhaAtual.getPassword()),
                        new String(jPasswordFieldRepitaNovaSenha.getPassword()));
                if (alterou > 0) {
                    JOptionPane.showMessageDialog(null, "A senha foi trocada com sucesso", "Senha trocada", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "Falha ao tentar trocar a senha, a senha atual está incorreta",
                            "Falha", JOptionPane.ERROR_MESSAGE);
                }
            } catch (PersistenciaException pex) {
                JOptionPane.showMessageDialog(null, pex.getMessage(), "Problemas", JOptionPane.WARNING_MESSAGE);
            }
        }
    }//GEN-LAST:event_confirmarButtonActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                TrocarSenhaDialog dialog = new TrocarSenhaDialog(null, true, 2);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // <editor-fold defaultstate="collapsed" desc="Declaration of class members">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton confirmarButton;
    private javax.swing.JButton fecharButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPasswordField jPasswordFieldNovaSenha;
    private javax.swing.JPasswordField jPasswordFieldRepitaNovaSenha;
    private javax.swing.JPasswordField jPasswordFieldSenhaAtual;
    private javax.swing.JTextField jTextFieldLogin;
    private javax.swing.JTextField jTextFieldNome;
    private javax.swing.JTextField jTextFieldUsuarioId;
    // End of variables declaration//GEN-END:variables
    // </editor-fold>

    private void montarUsuario(long usuarioId) {
        try {
            Usuario usuario = this.buscarUsuario(usuarioId);
            if (usuario != null) {
                jTextFieldUsuarioId.setText("" + usuario.getUsuarioId());
                jTextFieldNome.setText(usuario.getNome());
                jTextFieldLogin.setText(usuario.getLogin());
            }
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }

    private Usuario buscarUsuario(long usuarioId) throws PersistenciaException {
        ControleUsuario controle = new ControleUsuario();
        return controle.get(usuarioId);
    }

    @Override
    protected JRootPane createRootPane() {
        ActionListener closeListener = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                fecharButtonActionPerformed(e);
            }
        };
        KeyStroke stroke =
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane myRootPane = new JRootPane();
        myRootPane.registerKeyboardAction(
                closeListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        return myRootPane;
    }
}
